home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9860 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  102 lines

  1. Path: andrew.cmu.edu!sangp+
  2. From: "Sang Hyun Park, Shawn" <sangp+@andrew.cmu.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: How to read in binary file?
  5. Date: Mon,  4 Mar 1996 16:19:47 -0500
  6. Organization: Junior, Social & Decision Sciences, Carnegie Mellon, Pittsburgh, PA
  7. Message-ID: <4lCptna00iV5Q5qSZK@andrew.cmu.edu>
  8. NNTP-Posting-Host: po8.andrew.cmu.edu
  9.  
  10.  
  11. Could anyone with experience help me out?
  12. (in general and specific to msvc++ 2.1?)
  13.  
  14. I'm trying to read in a binary file into an array of fixed size.
  15. (the size of files i'm reading in is approx. 12k)
  16. I tried two different ways to read in the file, but i kept getting error
  17. messages.  could someone take a look it and suggest me what i'm doing wrong 
  18. or missing in the code?  Or please tell me if there is a better way to
  19. accomplish the task.
  20.  
  21. another question i have is, how can i display the content of the binary
  22. file on screen?  there is some header information in the input file that
  23. i need to skip, and i want to see how many bytes i need to skip from the
  24. beginning of the file.
  25.  
  26. i'm doing this under WinNT with MSVC++ 2.1
  27.  
  28. shawn
  29.  
  30.  
  31. trial-1
  32. ------------------------------------------------------------------------
  33. #include <fstream.h>
  34. #include <string.h>
  35.  
  36. void main(void)
  37. {
  38.  short buf[2000];    // arbitrary size for trial
  39.   
  40.  ifstream ins("filename.bin");
  41.  ins.get(buf, 2000);   *
  42.  
  43. }
  44. --------------------------------------
  45. "filename.bin" is the name of the binary file.
  46.  
  47. Error message i got was:
  48. * `class istream &istream::get(char*,int,char)': 
  49.        cannot convert parameter 1 from 'short[2000]' to 'char*'
  50.  
  51. so, i think it might be that i can only use ins.get to read in chars, and
  52. no binary chars.   is that correct?
  53.  
  54.  
  55.  
  56. trial-2
  57. -----------------------------------------------------------------------
  58. #include <afx.h>
  59. #include <iostream.h>
  60.  
  61. const size_t BUFSIZE = 256;
  62.  
  63. // Open a CFile associated with a specific file
  64.         CFile fin("Filename.bin", CFile::modeRead);
  65.  
  66. // Open test output file
  67.         CFile fout("output.xxx", CFile::modeWrite |
  68.                                CFile::modeCreate);
  69.  
  70. // Read from one file and write to the other
  71.         short buf[BUFSIZE];
  72.         int n = fin.Read(buf, BUFSIZE);
  73.  
  74.         while (n > 0)
  75.         {
  76.             fout.Write(buf, n);
  77.             n = fin.Read(buf, BUFSIZE);
  78.         };
  79.  
  80.         fin.Close();
  81.         fout.Close();
  82.  
  83. }
  84. ------------------------------------------
  85.  
  86. according to the book i have, i can read in binary information using the
  87. CFile class (as above), but i keep getting this error message. 
  88. i don't understand why.
  89.  
  90.  
  91. 1) nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
  92. "__beginthreadex"
  93. 2) nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
  94. "__endthreadex"
  95.  
  96.  
  97. i thought i wasn't dealing with thread. what is wrong with this method of
  98. reading in the file?
  99.  
  100.  
  101.  
  102.